<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    title="请假设置"
    :visible.sync="show"
    width="500px"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-row>
      <div style="font-weight: bold">请假类型设置</div>

      <el-row>
        <el-col
          :span="6"
          style="text-align: center; padding: 10px"
          v-for="(tag, index) in tags"
          :key="index"
        >
          <el-tag
            v-if="!tag.inputVisible"
            closable
            :disable-transitions="true"
            @close="handleClose(tag)"
          >
            {{ tag.leaveReason }}
          </el-tag>
          <el-input
            class="input-new-tag"
            v-if="tag.inputVisible"
            v-model="tag.leaveReason"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="handleInputConfirm(tag)"
            @blur="handleInputConfirm(tag)"
          >
          </el-input>
        </el-col>
        <el-col :span="24">
          <div style="float: right; margin: 20px">
            <el-button icon="el-icon-plus" @click="showInput">添加</el-button>
          </div>
        </el-col>
      </el-row>
    </el-row>
    <!-- <el-row>
      <div style="font-weight: bold">审批设置</div>
      <el-row>
        <el-col :span="6"
          ><span style="padding-left: 10px; line-height: 36px"
            >老师提交审批人:</span
          ></el-col
        >
        <el-col :span="12">
          <el-select v-model="value1" multiple placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
      </el-row>
    </el-row>

    <el-row style="margin-top: 20px">
      <el-col :span="6"
        ><span style="padding-left: 10px; line-height: 36px">
          学生提交审批人:</span
        ></el-col
      >
      <el-col :span="12">
        <el-select v-model="value1" multiple placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-col>
    </el-row> -->

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reasonPage, reasonCreate } from "@/api/study/clockRecord.js"
import { deleteAttendanceLeaveReason } from "@/api/study/attendanceLeaveReason.js"
import { randomUUID } from "crypto";
export default {
  name: "",
  components: {},
  data() {
    return {
      options: [{
        value: '选项1',
        label: '黄金糕'
      }, {
        value: '选项2',
        label: '双皮奶'
      }, {
        value: '选项3',
        label: '蚵仔煎'
      }, {
        value: '选项4',
        label: '龙须面'
      }, {
        value: '选项5',
        label: '北京烤鸭'
      }],
      value1: '',
      tags: [],
      // tags: [
      //   { name: "标签一", type: "" },
      //   { name: "标签二", type: "success" },
      //   { name: "标签三", type: "info" },
      //   { name: "标签四", type: "warning" },
      //   { name: "标签五", type: "danger",inputVisible:false},
      // ],
      inputVisible: false,
      submitting: false,
    };
  },
  created() {
    this.getTags();
  },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false,
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: "",
    },
    // tags:{
    //   type:Array
    // }
  },
  methods: {
    submit() {
      // consle.log(this.tags.at(-1))
      this.cancel();
    },
    getTags() {
      reasonPage({ pageNo: 1, pageSize: 99 }).then(res => {
        this.tags = res.data.list.reverse();
      })
    },
    async handleInputConfirm(tag) {

      if (tag.leaveReason == '') {
        if (this.tags[this.tags.length - 1]['leaveReason'] == "") {
          this.tags.splice(this.tags.length - 1, 1);
        }
        return
      }
      if (this.submitting) return;
      this.submitting = true;
      if (tag && tag.leaveReason) {
        tag.inputVisible = false;
        const res = await reasonCreate({ leaveReason: tag.leaveReason })
        if (res.code == 0) {
          this.getTags();
        }
        this.submitting = false;

      } else {
        // this.$message.error("请输入功能名称");
        this.submitting = false;
      }

    },
    handleClose(tag) {
      this.tags.forEach(async (e, i) => {
        if (e.leaveReason == tag.leaveReason) {
          const res = await deleteAttendanceLeaveReason(tag.id)
          if (res.code == 0) {
            this.getTags();
          }
          this.tags.splice(i, 1);
        }
      })
      // this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },
    showInput() {
      this.inputVisible = true;
      this.tags.push({ leaveReason: "", type: "danger", inputVisible: true });

      this.submitting = false;
      this.$nextTick(() => {
        // console.log(this.$refs.saveTagInput)
        this.$refs.saveTagInput.at(-1).focus();
      });
    },
    cancel() {
      this.$emit("update:show", false);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateClassroomInfo(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit("success");
          });
          return;
        }
        // 添加的提交
        createClassroomInfo(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit("success");
        });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
